package lingnan.cn.edu.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import lingnan.cn.edu.BaseAndImpl.ProductService;
import lingnan.cn.edu.User.Product;
import lingnan.cn.edu.util.CarItem;
import lingnan.cn.edu.util.DbAccess;

public class ProductServiceImpl  implements ProductService{

	@Override
	public List<Product> query() {
		List<Product> products = new ArrayList<Product>();
		Connection conn = DbAccess.getConnection();
		String SQL = "select * from product";
		
		PreparedStatement prep = null;
		ResultSet rs=null;
		try {
			
		    prep = conn.prepareStatement(SQL);
			rs = prep.executeQuery();
		    while(rs.next()) {
		    Product product = new Product();
		    product.setId(rs.getInt("id"));
		    product.setName(rs.getString("Name"));
		    product.setPrice(rs.getInt("Price"));
		    product.setImg(rs.getString("Img"));
		    product.setType(rs.getString("type"));
			
		    products.add(product);
		}
		
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		return products;
	}
	


	
public Product getProduct(int id) {
		
		Connection conn = DbAccess.getConnection();
		String SQL = "select * from product where id = ?";
		Product product = null;
		PreparedStatement prep = null;
		ResultSet rs=null;
		try {
			product = new Product();
			prep = conn.prepareStatement(SQL);
			prep.setInt(1, id);
			rs = prep.executeQuery();
			while(rs.next()) {
			    product.setId(rs.getInt("Id"));
				product.setName(rs.getString("Name"));
				product.setPrice(rs.getInt("Price"));
				product.setImg(rs.getString("Img"));
				product.setType(rs.getString("type"));
				}
			System.out.println(product);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		
		return product;
		
	}



	
}


